package com.xiaodimall.backsys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaodimall.backsys.mapper.AdminMapper;
import com.xiaodimall.backsys.pojo.AdminUser;
import com.xiaodimall.backsys.service.AdminService;
import com.xiaodimall.utils.MD5Util;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * @author wyc
 * @description
 * @ClassName AdminServiceImpl
 * @date 2022/11/25 10:00
 */
@Service
public class AdminServiceImpl implements AdminService {

    @Resource
    AdminMapper adminMapper;

    /**
     * 管理员登录功能的实现
     * @author WYC
     * @param userName
     * @param password
     * @param verifyCode
     * @param session
     * @return: 返回提示信息到页面
     * @date: 2022/11/25 11:01
     */
    @Override
    public String doAdminLogin(String userName, String password, String verifyCode, HttpSession session) {
        if (StringUtils.isEmpty(verifyCode)) {
            //返回提示信息
            session.setAttribute("errorMsg","验证码不能为空");
            return "admin/login";
        }
        if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)) {
            session.setAttribute("errorMsg","用户名或密码不能为空");
            return "admin/login";
        }
        String kaptchaCode = session.getAttribute("verifyCode") + "";
        if (StringUtils.isEmpty(kaptchaCode) || !verifyCode.equals(kaptchaCode)) {
            session.setAttribute("errorMsg","验证码错误");
            return "admin/login";
        }
        //查询是否有该管理员
        String passwordMd5 = MD5Util.MD5Encode(password, "UTF-8");
        AdminUser adminUser = adminMapper.selectOne(
                new QueryWrapper<AdminUser>().eq("login_user_name", userName)
                        .eq("login_password", passwordMd5));

        //有就绑定管理员id 后续有用
        if (adminUser != null){
            session.setAttribute("loginUserId",adminUser.getAdminUserId());
            return "redirect:/admin/to/index";
        } else {
            session.setAttribute("errorMsg","登录失败");
            return "admin/login";
        }
    }


}
